home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-01
/
lcppb.zip
/
LCPP04.ZIP
/
STACK.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-03
|
3KB
|
135 lines
// stack.cpp -- Implement a linked list stack
//#include <stream.hpp>
#include <iostream.h>
#include <stdlib.h>
//#include <disp.h>
#include "disp.h"
#include <ctype.h>
#define FALSE 0
#define TRUE 1
struct item {
int data; // Could be any data
item *next; // Points to next item in list
};
typedef item *itemptr; // Create itemptr data type
// Function prototypes
void push(itemptr newitem, itemptr &list);
void pop(itemptr &newitem, itemptr &list);
void showlist(itemptr p);
void display(void);
void additem(void);
void delitem(void);
void clearscreen(void);
itemptr avail; // The "avail" stack
itemptr itemlist; // The "item" stack
main()
{
int done = FALSE; // When TRUE, program ends
int c; // User command character
disp_open();
while (!done) {
display();
disp_move(24, 0);
disp_printf("A-dd, D-elete, Q-uit");
c = getch();
switch (toupper(c)) {
case 'A': additem(); break;
case 'D': delitem(); break;
case 'Q': done = TRUE;
}
}
}
// Push newitem onto list. Modifies list pointer.
void push(itemptr newitem, itemptr &list)
{
newitem->next = list; // Item's next now points to list
list = newitem; // List now points to new item
}
// Pop newitem from list, or create a new item if list is empty
void pop(itemptr &newitem, itemptr &list)
{
if (list == NULL) { // If list is empty...
newitem = new item; // Create new item
newitem->data = -1; // Initialize data to -1
} else { // Else...
newitem = list; // Pass back item at top of list
list = list->next; // Adjust list to next item
}
}
// Display contents of list addressed by p
void showlist(itemptr p)
{
while (p != NULL) {
// disp_printf("\n%d", p->data);
disp_move(wherey(), 0);
disp_printf("%d", p->data);
disp_eeol();
p = p->next; // Address next item in the list
}
}
// Display the avail and item stacks
void display(void)
{
clearscreen();
// disp_printf("\n\nAvail list:");
disp_move(wherey() + 1, 0);
disp_printf("Avail list:");
showlist(avail);
// disp_printf("\n\nItem list:");
disp_move(wherey() + 1, 0);
disp_printf("Item list:");
showlist(itemlist);
}
// Add item with random data to item stack
void additem(void)
{
itemptr newitem;
pop(newitem, avail); // Get or create new item
if (newitem->data < 0) // If this is a new item
newitem->data = rand(); // Assign random data to it
push(newitem, itemlist); // Put new item onto item stack
}
// Delete item from item stack. Save same item on avail stack
void delitem(void)
{
itemptr newitem;
pop(newitem, itemlist); // Get new item from item stack
push(newitem, avail); // Save item on avail stack
}
// Clear the display
void clearscreen(void)
{
disp_move(0, 0);
disp_eeop();
}
// Copyright (c) 1990 by Tom Swan. All rights reserved
// Revision 1.00 Date: 08/29/1990 Time: 08:59 pm
// Revision 1.01 Date: 07/03/1991 Time: 04:00 pm
// Converted for Borland C++ 2.0